using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TVT.Common.Exceptions;
using System.Threading;
using System.Collections.Generic;
using TVT.Common.Helpers;
using TVT.UI.Helpers;
using TVT.BizLayer.Entities;
using System.IO;
using Common.Entities;
using TVT.BizLayer.Managers;
using TVT.UI.EventArguments;

namespace TVT.UI.UserControls
{
    public partial class FaceRecognition : System.Web.UI.UserControl
    {

        public event EventHandler OnRecognitionFinish = null;

        public void RecognitionFinish(UserEventArgs args)
        {
            if (OnRecognitionFinish != null)
            {
                OnRecognitionFinish(this, args);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            User user = User.Find((Guid)Membership.GetUser().ProviderUserKey);
            if (!Page.IsPostBack)
            {
                //ViewState[WellKnownKeys.UserFacePointsKey] = new Dictionary<FacePoint.PositionEnum,FacePoint>();
                String imgUrl = ConfigurationManager.AppSettings[WellKnownKeys.TemporaryFilesPathKey].ToString() + user.UserName + ".jpg";
                String fullPath = imgUrl.Replace("~", AppDomain.CurrentDomain.BaseDirectory);
                if (File.Exists(fullPath))
                    this.imgUser.ImageUrl = imgUrl;
                if (!user.UserProfile.Male)
                    this.imgBase.ImageUrl = "~/App_Themes/NewDesign/Steps/woman1.jpg";
            }
            else
            { 
                Dictionary<FacePoint.PositionEnum, FacePoint> dic;
                if (!RecognitionManager.userPoints.TryGetValue(user.UserId, out dic))
                {
                    dic = new Dictionary<FacePoint.PositionEnum, FacePoint>();
                    RecognitionManager.userPoints.Add(user.UserId, dic);
                }
                //Dictionary<FacePoint.PositionEnum, FacePoint> dic = (Dictionary<FacePoint.PositionEnum, FacePoint>)ViewState[WellKnownKeys.UserFacePointsKey];
                int count = (dic.Count + 2);
                if (count > WellKnownKeys.FacePointsAmount)
                    count -= WellKnownKeys.FacePointsAmount;
                SetStepTex(count);
                String imgUrl = "~/App_Themes/NewDesign/Steps/";
                if (user.UserProfile.Male)
                    imgUrl += "man" + count.ToString() + ".jpg";
                else
                    imgUrl += "woman" + count.ToString() + ".jpg";
                
                this.imgBase.ImageUrl = imgUrl;
            }
        }

        private void SetStepTex(int step)
        {
            switch (step)
            { 
                case (int)FacePoint.PositionEnum.LeftEye:
                    lblStep.Text = "   Step 1 : Click on your right eye.";
                    break;
                case (int)FacePoint.PositionEnum.RightEye:
                    lblStep.Text = "   Step 2 : Click on your left eye.";
                    break;
                case (int)FacePoint.PositionEnum.Nose:
                    lblStep.Text = "   Step 3 : Click on your nose center.";
                    break;
                case (int)FacePoint.PositionEnum.Mouth:
                    lblStep.Text = "   Step 4 : Click on your mouth center.";
                    break;
                case (int)FacePoint.PositionEnum.HeadTop:
                    lblStep.Text = "   Step 5 : Click on your head top.";
                    break;
                case (int)FacePoint.PositionEnum.Chin:
                    lblStep.Text = "   Step 6 : Click on your chin.";
                    break;
                case (int)FacePoint.PositionEnum.RightEar:
                    lblStep.Text = "   Step 7 : Click on your left ear.";
                    break;
                case (int)FacePoint.PositionEnum.Jaw:
                    lblStep.Text = "   Step 8 : Click on your left jaw extreme.";
                    break;
            }
        }

        public void EraseMessage()
        {
            lblTransactionResultMessage.Visible = false;
        }

        protected void OnUserImageClick(Object sender, ImageClickEventArgs e)
        {
            int x = e.X;
            int y = e.Y;
            FacePoint facePoint = new FacePoint(x, y);

            User user = User.Find((Guid)Membership.GetUser().ProviderUserKey);
            Dictionary<FacePoint.PositionEnum, FacePoint> dic;
            if (!RecognitionManager.userPoints.TryGetValue(user.UserId, out dic))
            {
                dic = new Dictionary<FacePoint.PositionEnum, FacePoint>();
                RecognitionManager.userPoints.Add(user.UserId, dic);
            }
            //Dictionary<FacePoint.PositionEnum, FacePoint> dic = (Dictionary<FacePoint.PositionEnum, FacePoint>)ViewState[WellKnownKeys.UserFacePointsKey];
            if (dic.Count < WellKnownKeys.FacePointsAmount)
            {
                FacePoint.PositionEnum pointItem = (FacePoint.PositionEnum)Enum.ToObject(typeof(FacePoint.PositionEnum), dic.Count + 1);
                bool isMale = false;
                if (dic.Count == 0)
                {
                    isMale = user.UserProfile.Male;
                    facePoint.IsMale = isMale;
                }
                else
                {
                    isMale = dic[FacePoint.PositionEnum.LeftEye].IsMale;
                }
                dic.Add(pointItem, facePoint);

                if (dic.Count == WellKnownKeys.FacePointsAmount)
                {
                    RecognitionManager.Recognize(user, dic, isMale);
                    //ViewState[WellKnownKeys.UserFacePointsKey] = new Dictionary<FacePoint.PositionEnum, FacePoint>();
                    RecognitionManager.userPoints.Remove(user.UserId);
                    //RecognitionFinish(new UserEventArgs());
                    Response.Redirect("MatchFinder.aspx");
                }
                else
                {
                    RecognitionManager.userPoints[user.UserId] = dic;
                    //ViewState[WellKnownKeys.UserFacePointsKey] = dic;
                }
            }
        }
    }
}